package com.yc.project3;

import com.yc.project3.annotations.Constraints;
import com.yc.project3.annotations.DBTable;
import com.yc.project3.annotations.SQLType;
import com.yc.project3.commons.DbProperties;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zwei
 * @create 2024-07-26 19:39
 */
public class CreatorTable {
    public static void main(String[] args) throws SQLException {
        Connection con=getConnection();//获取联接
        PreparedStatement pstmt=null;
        CreatorTable ct=new CreatorTable();
        List<String>list=new ArrayList<>();
        list.add("com.yc.project3.bean.Product");
        List<String>sql=ct.getTableName(list);
        System.out.println(sql);
        for (String s:sql){
            pstmt=con.prepareStatement(s);
            System.out.println(pstmt.execute());
        }
    }

    public static Connection getConnection() throws SQLException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        DbProperties p=DbProperties.getInstance();
        Connection con= DriverManager.getConnection(  p.getProperty("mysqlurl"),
                p.getProperty("mysqluname"),
                p.getProperty("mysqlpwd")
        );
        return con;
    }


    /**
     * 根据给定的类，检索是否使用了DBTable注解  如果有返回其建表的语句
     * @param className
     * @return
     */
    public List<String>getTableName(List<String>className){
        List<String>tableName=new ArrayList<>();
        String sql="create table ";
        if (className!=null&&className.size()>0){
            for (String cn:className){
                try {
                    Class<?> cl=Class.forName(cn);  //根据类名加载类
                    DBTable dbTable=cl.getAnnotation(DBTable.class);    //检查该类是否有DBTable注解
                    if (dbTable!=null){ //如果有  则获取其名字
                        sql+=dbTable.name()+"(";
                        //生成所有的列信息
                        sql=getFields(sql,cl);
                        tableName.add(sql);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return tableName;
    }

    /**
     * 根据给定的类，返回该类下的所有字段信息
     * @param sql：要拼接在后面的sql语句
     * @param cl：要搜索的类
     * @return
     */
    private String getFields(String sql,Class<?>cl){
        for (Field fd:cl.getDeclaredFields()){  //循环类中所有的属性
            Annotation[]anns=fd.getDeclaredAnnotations();   //获取该属性上的域注解
            if (anns.length>0){ //如果该属性上有注解
                for (Annotation ans:anns){  //判断是不是SQLType类型的注解
                    if (ans instanceof SQLType){
                        SQLType sqlType= (SQLType) ans;
                        sql+=fd.getName()+" "+sqlType.dbtype();
                        if (sqlType.constraints().primarKey()){ //如果是主键
                            sql+=" primary key";
                        }
                        if (!sqlType.constraints().allowNull()){    //如果不允许为空
                            sql+=" not null ";
                        }
                        if (sqlType.constraints().unique()){    //如果唯一则
                            sql+=" unique";
                        }
                        sql+=",";
                    }

                }
            }
        }
        sql=sql.substring(0,sql.lastIndexOf(","))+")";
        return sql;
    }

}
